package com.wx.tnsadmin.controller;


import com.wx.common.api.CommonResult;
import com.wx.common.api.ResultCode;
import com.wx.tnsadmin.model.UmsAuthModel;
import com.wx.tnsadmin.service.UmsAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Author:LLH
 * @Date:2020/8/10
 */

@RequestMapping(value = "/auth")
@RestController
@Api(tags = "后台权限管理")
public class UmsAuthController {

    @Autowired
    private UmsAuthService umsAuthService;

    @ApiOperation(value = "后台用户权限查询", notes = "后台用户权限查询")
    @RequestMapping(value = "/findAllAuth", method = RequestMethod.GET)
    public CommonResult findAllAuth () {
        List<UmsAuthModel> allAuth = umsAuthService.findAllAuth ( );
        try {
            if (CollectionUtils.isEmpty ( allAuth )) {
                return CommonResult.failed ( "用户权限查询失败!" );
            } else {
                return CommonResult.success ( allAuth,"用户权限查询成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }



    @ApiOperation(value = "后台用户ID权限查询", notes = "后台用户ID权限查询")
    @RequestMapping(value = "/findByAuthId", method = RequestMethod.GET)
    public  CommonResult findByAuthId(@RequestParam(required = false) Integer id){
        UmsAuthModel byAuthId = umsAuthService.findByAuthId ( id );
        try {
            if(byAuthId==null){
                return CommonResult.failed ( "用户权限ID查询失败!" );
            }else {
                return CommonResult.success ( byAuthId,"用户权限ID查询成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }


    @PreAuthorize("hasAuthority('tns:auth:saveAuth')")
    @ApiOperation(value = "后台用户新增权限", notes = "后台用户新增权限")
    @RequestMapping(value = "/saveAuth", method = RequestMethod.POST)
    public CommonResult saveAuth(UmsAuthModel umsAuthModel){
        Integer integer = umsAuthService.saveAuth ( umsAuthModel );
        try {
            if(integer<0){
                return CommonResult.failed ( "后台用户新增权限失败!" );
            }else {
                return CommonResult.success ( integer,"后台用户新增权限成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @PreAuthorize("hasAuthority('tns:auth:updateAuth')")
    @ApiOperation(value = "后台用户修改权限", notes = "后台用户修改权限")
    @RequestMapping(value = "/updateAuth", method = RequestMethod.POST)
    public CommonResult updateAuth(UmsAuthModel umsAuthModel){
        Integer integer = umsAuthService.updateAuth ( umsAuthModel );
        try {
            if(integer<0){
                return CommonResult.failed ( "后台用户修改权限失败!" );
            }else {
                return CommonResult.success ( integer,"后台用户修改权限成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @PreAuthorize("hasAuthority('tns:auth:deleteAuthById')")
    @ApiOperation(value = "后台用户删除权限", notes = "后台用户删除权限")
    @RequestMapping(value = "/deleteAuthById", method = RequestMethod.DELETE)
    public CommonResult deleteAuthById(@RequestParam(required = false) Integer id) {
        Integer integer = umsAuthService.deleteAuthById ( id );
        try {
            if(integer<0){
                return CommonResult.failed ( "后台用户删除权限失败!" );
            }else {
                return CommonResult.success ( integer,"后台用户删除权限成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

}
